package com.mall.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.dubbo.mapper.UmsMenuMapper;
import com.mall.model.domain.UmsMenu;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author xyiaao
 * @description 针对表【ums_menu(后台菜单表)】的数据库操作Api实现
 * @createDate 2022-03-29 19:46:09
 */
@DubboService
public class UmsMenuApiImpl extends ServiceImpl<UmsMenuMapper, UmsMenu>
        implements UmsMenuApi {

    @Autowired
    private UmsMenuMapper umsMenuMapper;

    @Override
    public List<UmsMenu> listUmsMenuByRoleId(Long roleId) {

        return umsMenuMapper.listUmsMenuByRoleId(roleId);
    }

    //添加后台菜单
    public void saveMenu(UmsMenu umsMenu) {

        umsMenuMapper.insert(umsMenu);
    }

    //分页查询后台菜单
    public Page<UmsMenu> findById(Integer parentId, Integer pageNum, Integer pageSize) {

        Page<UmsMenu> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<UmsMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(parentId != null, UmsMenu::getParentId, parentId);

        Page<UmsMenu> umsMenuPage = umsMenuMapper.selectPage(page, lqw);
        return umsMenuPage;
    }

    //根据ID删除后台菜单
    public void deleteUmsMenu(Integer id) {

        umsMenuMapper.deleteById(id);

    }


    //查询父级菜单
    public List<UmsMenu> findByParentIds() {

        LambdaQueryWrapper<UmsMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsMenu::getLevel, 0).eq(UmsMenu::getHidden, 0);

        return umsMenuMapper.selectList(lqw);
    }

    //查询子级信息
    public List<UmsMenu> findByChiledIds(List<Long> ids) {

        LambdaQueryWrapper<UmsMenu> lqw = new LambdaQueryWrapper<>();
        lqw.in(CollUtil.isNotEmpty(ids), UmsMenu::getParentId, ids)
                .eq(UmsMenu::getHidden, 0);

        List<UmsMenu> list = umsMenuMapper.selectList(lqw);


        return list;
    }


    //修改菜单列表
    public void updateMenu(Integer id, UmsMenu umsMenu) {

        LambdaQueryWrapper<UmsMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null, UmsMenu::getId, id);
        umsMenuMapper.update(umsMenu, lqw);
    }

    //根据ID获取菜单详情
    public UmsMenu findId(Integer id) {

        return umsMenuMapper.selectById(id);
    }

    @Override
    public List<UmsMenu> findids(Integer id) {

        LambdaQueryWrapper<UmsMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsMenu::getParentId, id);

        return umsMenuMapper.selectList(lqw);
    }

}




